package com.socialsoftware.ui.settings;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.socialsoftware.Page.LoginActivity;
import com.socialsoftware.R;
import com.socialsoftware.util.SessionManager;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        // Setup toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("设置");
        }

        // Setup click listeners for settings items
        setupAccountSecurity();
        setupPrivacySettings();
        setupNotificationSettings();
        setupGeneralSettings();
        setupAboutSettings();
        setupLogoutButton();
    }

    private void setupAccountSecurity() {
        View accountSecurityItem = findViewById(R.id.settings_account_security);
        accountSecurityItem.setOnClickListener(v -> {
            // Navigate to account security settings
            // Intent intent = new Intent(this, AccountSecurityActivity.class);
            // startActivity(intent);
        });
    }

    private void setupPrivacySettings() {
        View privacyItem = findViewById(R.id.settings_privacy);
        privacyItem.setOnClickListener(v -> {
            // Navigate to privacy settings
            // Intent intent = new Intent(this, PrivacySettingsActivity.class);
            // startActivity(intent);
        });
    }

    private void setupNotificationSettings() {
        View notificationItem = findViewById(R.id.settings_notifications);
        notificationItem.setOnClickListener(v -> {
            // Navigate to notification settings
            // Intent intent = new Intent(this, NotificationSettingsActivity.class);
            // startActivity(intent);
        });
    }

    private void setupGeneralSettings() {
        View generalItem = findViewById(R.id.settings_general);
        generalItem.setOnClickListener(v -> {
            // Navigate to general settings
            // Intent intent = new Intent(this, GeneralSettingsActivity.class);
            // startActivity(intent);
        });
    }

    private void setupAboutSettings() {
        View aboutItem = findViewById(R.id.settings_about);
        TextView versionText = findViewById(R.id.settings_version);
        
        // Set version info
        versionText.setText("版本 1.0.0");
        
        aboutItem.setOnClickListener(v -> {
            // Navigate to about page
            // Intent intent = new Intent(this, AboutActivity.class);
            // startActivity(intent);
        });
    }

    private void setupLogoutButton() {
        TextView logoutButton = findViewById(R.id.settings_logout);
        logoutButton.setOnClickListener(v -> showLogoutConfirmDialog());
    }

    private void showLogoutConfirmDialog() {
        new AlertDialog.Builder(this)
                .setTitle("退出登录")
                .setMessage("确定要退出登录吗？")
                .setPositiveButton("退出", (dialog, which) -> {
                    // Perform logout
                    SessionManager.getInstance(this).logout();
                    
                    // Return to login screen and clear activity stack
                    Intent intent = new Intent(this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                    finish();
                })
                .setNegativeButton("取消", null)
                .show();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 